﻿using System;
using System.Collections.Generic;
using System.Threading;
using CosyTrade.ClientForm.Printer;
using CosyTrade.Financial;
using CosyTrade.Organization;
using CosyTrade.Trade;
using CosyTrade.UI;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Trade
{
    public partial class Export_Unreceive : FormBase
    {
        public Export_Unreceive()
        {
            InitializeComponent();
        }

        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem("ExportBillDetail", Resources.table, "出口单明细", true, true, true,BillDetailCallback);

            ToolbarItems.AddCommandItem("PrintExportBill", Resources.printer, "打印出口单", true, true, true, PrintExportCallback);

            ToolbarItems.AddCommandItem("RefreshExportBill", Resources.reload, "刷新列表", true, true, true,RefreshCallback);

            ToolbarItems.AddCommandItem("Close", Resources.door_out, "关闭", true, true, true,CloseCallback);

        }

        ExportBillBase GetCurrentObj()
        {
            ExportBillBase bill = bindingSource1.Current as ExportBillBase;
            return bill;
        }


        private void CloseCallback(object sender, EventArgs e)
        {
            Close();
        }
        
        private void BillDetailCallback(object sender, EventArgs e)
        {
            OpenCurrentDetail();
        }

        private void OpenCurrentDetail()
        {
            ExportBillBase bill = GetCurrentObj();
            if(bill==null)
            {
                Functions.ShowConfigBox("请选择要查看的出口单");
                return;
            }
            ExportDetailForm form = new ExportDetailForm(bill.PrimaryKey);
            //form.ReadOnlyMode = true;
            form.Show();
        }

        private void PrintExportCallback(object sender, EventArgs e)
        {
            ExportBillBase exportBill = bindingSource1.Current as ExportBillBase;
            if (exportBill == null)
            {
                Functions.ShowMessageBox("请选择要打印的出口提单");
                return;
            }
            PrintExport printExportBill = new PrintExport(exportBill);
            printExportBill.ShowDialog();
        }

        private void RefreshCallback(object sender, EventArgs e)
        {
            ((ThreadStart)RefreshDataSourceInvoke).BeginInvoke(null, null);
        }

        private void RefreshDataSourceInvoke()
        {
            IList<ExportBillBase> shippingList =
                Operation.TradeSystem.GetFinishExportBillByExporter(new DateTime(2000, 1, 1),
                                                                    new DateTime(2020, 12, 31),
                                                                    FinishExportBillType
                                                                        .Unreceive);
            bindingSource1.DataSource = shippingList;
        }

        private void Export_Unreceive_Load(object sender, EventArgs e)
        {
            colTotal.Visible = colTotal2.Visible = GlobalInfo.CurrentRole.Powerset.Trade_ViewExportBillPrice;
            ((ThreadStart)RefreshDataSourceInvoke).BeginInvoke(null, null);
        }

        private void rptCompany_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            Company cmp = e.Value as Company;
            if (cmp != null) e.DisplayText = cmp.Caption;
        }

        private void rptEmployee_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            Employee emp = e.Value as Employee;
            if (emp != null) e.DisplayText = emp.EmpName;
        }

        private void rptRate_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            ExchangeRate rate = e.Value as ExchangeRate;
            if (rate != null) e.DisplayText = rate.Exchange.ToString();
        }

        private void gridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left || e.Clicks < 2) return;
            GridHitInfo hitInfo = gridView1.CalcHitInfo(e.Location);
            if (!hitInfo.InRow) return;
            OpenCurrentDetail();
        }

        private void gridView1_KeyUp(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = false;
            if (e.KeyCode == Keys.Enter) OpenCurrentDetail();
        }
    }
}
